package com.flyzing.genl.service;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.flyzing.genl.dto.MapperFile;
import com.flyzing.genl.po.ColumnPO;
import com.flyzing.genl.po.TablePO;
import com.flyzing.genl.util.GenConfigHelper;
import com.flyzing.genl.util.StringUtil;
import com.flyzing.genl.util.TemplateHelper;

/**
 * @author yaoguangyao
 * @Date 2020年12月30日
 */
public class GenMapperFile implements IGenFile {

	@Override
	public void generate(TablePO table, List<ColumnPO> columns) {
		String packagesMapper = GenConfigHelper.getProperty("package.mapper");
		String clazzName = StringUtil.convertUnderline(table.getTableName(), true);
		String path = JAVA_ROOT_PATH + "/" + packagesMapper.replaceAll("\\.", "/") + "/" + clazzName + "Mapper.java";

		// mapper默认不重写
		File file = new File(path);
		if (file.exists()) {
			return;
		}

		String packagesPO = GenConfigHelper.getProperty("package.po");
		String poFullName = packagesPO + "." + clazzName;

		MapperFile mapperFile = new MapperFile();
		mapperFile.setPack(packagesMapper);
		mapperFile.setAuthor(AUTHOR);
		mapperFile.setClazzComment(table.getTableComment());
		mapperFile.setPoFullName(poFullName);
		mapperFile.setPoName(clazzName);
		mapperFile.setClazzName(clazzName + "Mapper");
		mapperFile.setCreateDate(DATE_FORMAT.format(new Date()));
		
		file.getParentFile().mkdirs();
		TemplateHelper.getInstance().process("Mapper", path, mapperFile);
	}

}
